package leetcode101.bit_operation;

/**
 * @author Synhard
 * @version 1.0
 * @Class Code4
 * @Description 合并两个排序的链表并且去重
 * @tel 13001321080
 * @email 823436512@qq.com
 * @date 2021-05-01 10:04
 */
public class Code4 {

    static class ListNode {
        int val;
        ListNode next;
        ListNode(int x) { this.val = x; }
    }

    public static void main(String[] args) {
        ListNode l1 = new ListNode(1);
        l1.next = new ListNode(2);
        l1.next.next = new ListNode(2);
        l1.next.next.next = new ListNode(2);
        l1.next.next.next.next = new ListNode(2);
        l1.next.next.next.next.next = new ListNode(3);
        l1.next.next.next.next.next.next = new ListNode(3);
        ListNode l2 = new ListNode(1);
        l2.next = new ListNode(2);
        l2.next.next = new ListNode(2);
//        l2.next.next.next = new ListNode(3);
        ListNode l3 = mergeTwoLists(l1, l2);
        System.out.println(1);
    }

    public static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if (l1 == null || l2 == null) {
            if (l1 == null) {
                ListNode p = l2;
                while (p != null) {
                    if (p.next.val == p.val) {
                        p.next = p.next.next;
                    }
                    p = p.next;
                }
                return l2;
            } else {
                ListNode p = l1;
                while (p != null) {
                    if (p.next.val == p.val) {
                        p.next = p.next.next;
                    }
                    p = p.next;
                }
                return l1;
            }
        }

        if (l1.val < l2.val) {
            ListNode p = mergeTwoLists(l1.next, l2);
            while (p.next != null) {
                if (l1.val == p.val) {
                    p = p.next;
                } else {
                    break;
                }
            }
            l1.next = p;
            return l1;
        } else if (l1.val > l2.val) {
            ListNode p = mergeTwoLists(l1, l2.next);
            while (p.next != null) {
                if (l2.val == p.val) {
                    p = p.next;
                } else {
                    break;
                }
            }
            l2.next = p;
            return l2;
        } else {
            ListNode p = mergeTwoLists(l1.next, l2.next);
            while (p.next != null) {
                if (l1.val == p.val) {
                    p = p.next;
                } else {
                    break;
                }
            }
            l1.next = p;
            return l1;
        }
    }
}
